package jinyilw.swing;

import jinyilw.common.file.FileTools;
import jinyilw.common.log.LogTools;
import jinyilw.swing.icon.LazyScaleIcon;

import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jinyi
 */
public class ResourceManager
{
	private static final ConcurrentHashMap<String, Object> resources =
			new ConcurrentHashMap<>();

	public static BufferedImage getBufferedImage(File file)
	{
		if (file == null || !file.isFile())
			return null;
		String name = file.getAbsolutePath();
		try
		{
			String key = "bimg:" + name;
			if (resources.containsKey(key))
				return (BufferedImage) resources.get(key);
			BufferedImage bimg = ImageIO.read(file);
			if (bimg != null)
				resources.put(key, bimg);
			return bimg;
		} catch (Exception e)
		{
			LogTools.debug(
					"ResourceManager.getBufferedImage: 获取图像错误！" + file.getAbsolutePath(),
					e);
			return null;
		}
	}

	public static BufferedImage getBufferedImage(URL url)
	{
		if (url == null)
			return null;
		String name = url.getPath();
		try
		{
			String key = "bimg:" + name;
			if (resources.containsKey(key))
				return (BufferedImage) resources.get(key);
			BufferedImage bimg = ImageIO.read(url);
			if (bimg != null)
				resources.put(key, bimg);
			return bimg;
		} catch (Exception e)
		{
			LogTools.debug("ResourceManager.getBufferedImage: 获取图像错误！" + name, e);
			return null;
		}
	}

	public static BufferedImage getBufferedImage(Class<?> loadClass, String path)
	{
		if (path == null)
			return null;
		if (loadClass == null)
			return getBufferedImage(FileTools.getUserFile(path));
		return getBufferedImage(loadClass.getResource(path));
	}

	public static ArrayList<BufferedImage> getImagesInDirectory(File directory)
	{
		ArrayList<BufferedImage> images = new ArrayList<>();
		if (directory != null && directory.isDirectory())
		{
			File[] imgFiles = directory.listFiles();
			if (imgFiles != null)
			{
				for (File imgFile : imgFiles)
				{
					if (imgFile.isFile())
						images.add(getBufferedImage(imgFile));
				}
			}
		}
		return images;
	}

	public static LazyScaleIcon getResLazyScaleIcon(Class<?> loadClass, String path)
	{
		if (path == null)
			return null;
		return LazyScaleIcon.getIcon(loadClass, path);
	}

	public static Image getImage(File file)
	{
		if (file == null || !file.isFile())
			return null;
		try
		{
			return getImage(file.toURL());
		} catch (MalformedURLException e)
		{
			LogTools.debug("ResourceManager.getImage: 获取图像错误！" + file.getName(), e);
			return null;
		}
	}

	public static Image getImage(Class<?> loadClass, String path)
	{
		if (path == null)
			return null;
		if (loadClass == null)
			return getImage(FileTools.getUserFile(path));
		return getImage(loadClass.getResource(path));
	}

	public static Image getImage(URL url)
	{
		if (url == null)
			return null;
		String name = url.getPath();
		try
		{
			String key = "img:" + name;
			if (resources.containsKey(key))
				return (Image) resources.get(key);
			Image image = Toolkit.getDefaultToolkit().getImage(url);
			if (image != null)
				resources.put(key, image);
			return image;
		} catch (Exception e)
		{
			LogTools.debug("ResourceManager.getImage: 获取图像错误！" + name, e);
			return null;
		}
	}

	//	public static LazyScaleIcon getResLazyScaleIcon(String path)
	//	{
	//		if (path == null)
	//			return null;
	//		try
	//		{
	//			path = path.replace('\\', '/');
	//			if (path.startsWith("/"))
	//				path = path.substring(1);
	//			String key = "sico:" + path;
	//			if (resources.containsKey(key))
	//				return (LazyScaleIcon) resources.get(key);
	//			LazyScaleIcon ico = new LazyScaleIcon(ResourceManager.class, path);
	//			if (ico != null)
	//				resources.put(key, ico);
	//			return ico;
	//		} catch (Exception e)
	//		{
	//			LogTools.debug("ResourceManager.getResLazyScaleIcon: 获取图像错误！" + path, e);
	//			return null;
	//		}
	//	}
}
